//
//  HistoryCell.m
//  ZYTS-iPhone
//
//  Created by yuzhou on 13-1-11.
//  Copyright (c) 2013年 wzyk. All rights reserved.
//

#import "HistoryCell.h"

@implementation HistoryCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)updateDisplay
{
    NSArray *playlist = [self.historyInfo valueForKey:@"playlist"];
    int playindex = [[self.historyInfo valueForKey:@"index"] intValue];
    NSDictionary *item = [playlist objectAtIndex:playindex];
    int seconds = [[self.historyInfo valueForKey:@"time"] intValue];
    
    titleLabel.text = [NSString stringWithFormat:@"%@ %@",[item valueForKey:@"item_name"],[item valueForKey:@"sub_name"]];
    timeLabel.text = [NSString stringWithFormat:@"%@",[self stringFromSeconds:seconds]];
    
    if (self.index % 2 == 0) {
        self.contentView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.1];
    }
    else {
        self.contentView.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.1];
    }
}

- (NSString *)stringFromSeconds:(int)seconds
{
    int hour = seconds / 3600;
    int minute = seconds % 3600 / 60;
    int second = seconds % 60;
    if (hour > 0) {
        return [NSString stringWithFormat:@"%2d:%02d:%02d",hour,minute,second];
    }
    else {
        return [NSString stringWithFormat:@"%02d:%02d",minute,second];
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
    if (selected) {
        if (self.editing) {
            if (multiSelected) {
                checkImageView.image = [UIImage imageNamed:@"checkbox"];
                [_historyToDeleteArray removeObject:[_historyInfo objectForKey:@"item_id"]];
            }
            else {
                checkImageView.image = [UIImage imageNamed:@"checkbox_selected"];
                [_historyToDeleteArray addObject:[_historyInfo objectForKey:@"item_id"]];
            }
            multiSelected = !multiSelected;
        }
    }
}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    if (editing) {
        [UIView animateWithDuration:animated ? 0.5 : 0 animations:^{
            CGRect rect = containerView.frame;
            rect.origin.x = 0;
            containerView.frame = rect;
            
            rect = titleLabel.frame;
            rect.size.width = 268;
            titleLabel.frame = rect;
            
            timeLabel.alpha = 0;
        }];
        
        if ([_historyToDeleteArray containsObject:[_historyInfo objectForKey:@"item_id"]]) {
            checkImageView.image = [UIImage imageNamed:@"checkbox_selected"];
            multiSelected = YES;
        }
        else {
            checkImageView.image = [UIImage imageNamed:@"checkbox"];
            multiSelected = NO;
        }
    }
    else {
        [UIView animateWithDuration:animated ? 0.5 : 0 animations:^{
            CGRect rect = containerView.frame;
            rect.origin.x = -24;
            containerView.frame = rect;
            
            timeLabel.alpha = 1;
        }];
        
        [CATransaction begin];
        CATransition *animation = [CATransition animation];
        animation.duration = .5;
        [animation setFillMode:kCAFillModeForwards];
        [animation setType:kCATransitionFade];
        
        CGRect rect = titleLabel.frame;
        rect.size.width = 241;
        titleLabel.frame = rect;
        
        [titleLabel.layer addAnimation:animation forKey:@"titleLabel"];
        [CATransaction commit];
        
        multiSelected = NO;
    }
}

- (void)prepareForReuse
{
    multiSelected = NO;
}

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
    [super setHighlighted:highlighted animated:animated];
    
    [UIView animateWithDuration:animated?0.5:0 animations:^{
        if (highlighted) {
            self.contentView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.6];
        }
        else {
            if (self.index % 2 == 0) {
                self.contentView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.1];
            }
            else {
                self.contentView.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.1];
            }
        }
    }];
}

- (void)dealloc {
    [titleLabel release];
    [timeLabel release];
    [containerView release];
    [checkImageView release];
    [super dealloc];
}
@end
